﻿using System;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Autofac.Configuration;
using Autofac.Integration.Mvc;
using log4net;
using log4net.Config;

namespace SmallBusinessKit
{
    /*
    This code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. 
    It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. 
    It's recommended that you add security and error-handling code to make your projects more secure and robust. 
    This code "AS IS" with no warranties.
    */

    /* Original Starter Kit: http://www.asp.net/downloads/starter-kits/small-business */

    public class MvcApplication : System.Web.HttpApplication
    {
        #region Properties

        private readonly ILog _log = LogManager.GetLogger(typeof(MvcApplication));

        #endregion

        #region Methods

        protected void Application_Start()
        {
            XmlConfigurator.Configure();

            _log.Info("*".PadRight(45, '*'));
            _log.Info("Initializing application...");
            
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            
            var autofac   = new ContainerBuilder();
            autofac.RegisterModule(new ConfigurationSettingsReader("autofac"));
            var container = autofac.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
            _log.Error(sender);
            _log.Error(e);
        }

        #endregion
    }
}
